﻿using System.Data.Entity;
using Medora.Core;
using Medora.Model.Basic;
using Medora.Model.Classifiers;
using Medora.Model.Institutions;
using Medora.Model.Persons;
using Medora.Model.Security;

namespace Medora.Data
{
    public class MedoraDbContext : DbContext
    {
        static MedoraDbContext()
        {
            Database.SetInitializer<MedoraDbContext>(null);
        }

        public MedoraDbContext(): base(Settings.ConnectionString)
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Position>().Map(m => m.ToTable("Medora_Positions"));
            modelBuilder.Entity<Speciality>().Map(m => m.ToTable("Medora_Specialities"));
            modelBuilder.Entity<Country>().Map(m => m.ToTable("Medora_Countries"));
            modelBuilder.Entity<Region>().Map(m => m.ToTable("Medora_Regions"));
            modelBuilder.Entity<Place>().Map(m => m.ToTable("Medora_Places"));
            modelBuilder.Entity<Address>().Map(m => m.ToTable("Medora_Addresses"));
            modelBuilder.Entity<Address>().HasOptional(m => m.Region);
            modelBuilder.Entity<Address>().HasRequired(m => m.Country);
            modelBuilder.Entity<Address>().HasOptional(m => m.Place);

            
            modelBuilder.Entity<Room>().Map(m => m.ToTable("Medora_Rooms"));
            modelBuilder.Entity<Room>().HasRequired(c => c.Office);
            
            modelBuilder.Entity<Department>().Map(m => m.ToTable("Medora_Departments"));
            modelBuilder.Entity<Department>().HasRequired(c => c.Institution);

            modelBuilder.Entity<Office>().Map(m => m.ToTable("Medora_Offices"));
            modelBuilder.Entity<Office>().HasRequired(c => c.Institution);

            modelBuilder.Entity<Person>().Map(m => m.ToTable("Medora_Persons"));
            modelBuilder.Entity<Person>().HasRequired(c => c.Address);

            modelBuilder.Entity<PersonProviderInstitution>().Map(m => m.ToTable("Medora_PersonsInstitutions"));
            modelBuilder.Entity<Provider>().Map(m => m.ToTable("Medora_Providers"));
            modelBuilder.Entity<Provider>().HasMany(t => t.PersonProviderInstitutions);


            modelBuilder.Entity<Sector>().Map(m => m.ToTable("Medora_Sectors"));

            modelBuilder.Entity<Institution>().Map(m => m.ToTable("Medora_Institutions"));
            modelBuilder.Entity<Institution>().HasRequired(c => c.Address);
            modelBuilder.Entity<Institution>().HasMany(t => t.PersonProviderInstitutions);

            
            //modelBuilder.Entity<Provider>();//.HasRequired(c => c.Speciality);  
            //modelBuilder.Entity<Provider>();//.HasRequired(c => c.Position);  
            modelBuilder.Entity<Provider>().HasRequired(c => c.Department);
            modelBuilder.Entity<UserProfile>().Map(m => m.ToTable("Medora_UserProfiles"));

            modelBuilder.Entity<ScheduleType>().Map(m => m.ToTable("Medora_Cl_ScheduleTypes"));

            modelBuilder.Entity<ScheduleTemplate>().Map(m => m.ToTable("Medora_ScheduleTemplates"));
            modelBuilder.Entity<ScheduleTemplateItem>().Map(m => m.ToTable("Medora_ScheduleTemplateItems"));

            modelBuilder.Entity<ScheduleActivity>().Map(m => m.ToTable("Medora_ScheduleActivities"));
            modelBuilder.Entity<ScheduleItem>().Map(m => m.ToTable("Medora_ScheduleItems"));
            modelBuilder.Entity<ScheduleItem>().HasOptional(o => o.ScheduleActivity);//.WithOptionalDependent().WillCascadeOnDelete(true); ;
            modelBuilder.Entity<ScheduleItem>().HasRequired(o => o.ScheduleType);//.WithOptionalDependent().WillCascadeOnDelete(true); ;

            modelBuilder.Entity<Visit>().Map(m => m.ToTable("Medora_Visits"));
            modelBuilder.Entity<Visit>().HasRequired(o => o.Person);

            modelBuilder.Entity<Allergy>().Map(m => m.ToTable("Medora_Allergies")).HasRequired(a=>a.Person);
            modelBuilder.Entity<BadHabits>().Map(m => m.ToTable("Medora_BadHabits")).HasRequired(a=>a.Person);
            modelBuilder.Entity<BloodPressureMeasurement>().Map(m => m.ToTable("Medora_BloodPressureMeasurements")).HasRequired(a => a.Visit);
            modelBuilder.Entity<HeightWeightMeasurement>().Map(m => m.ToTable("Medora_HeightWeightMeasurements")).HasRequired(a => a.Visit);
            modelBuilder.Entity<DiagnosisInVisit>().Map(m => m.ToTable("Medora_DiagnosisInVisit")).HasRequired(a => a.Visit);
            modelBuilder.Entity<FamilyHistory>().Map(m => m.ToTable("Medora_FamilyHistory")).HasRequired(a => a.Person);
            modelBuilder.Entity<Medication>().Map(m => m.ToTable("Medora_Medications")).HasRequired(a => a.Person);
            modelBuilder.Entity<ProcedureInVisit>().Map(m => m.ToTable("Medora_ProceduresInVisit")).HasRequired(a => a.Visit);
            modelBuilder.Entity<ServiceInVisit>().Map(m => m.ToTable("Medora_ServicesInVisit")).HasRequired(a => a.Visit);
            modelBuilder.Entity<Referral>().Map(m => m.ToTable("Medora_Referrals")).HasRequired(a => a.Person);



            /* modelBuilder.Entity<ScheduleItem>().Map(m => m.ToTable("Medora_ScheduleItems"));
            modelBuilder.Entity<ScheduleItem>().HasRequired(c => c.Provider);
            modelBuilder.Entity<ScheduleActivity>().Map(m => m.ToTable("Medora_ScheduleActivities"));*/


            /* modelBuilder.Entity<BankAccount>().Map(m => m.ToTable("EFactura_TaxpayerBanks"));
            modelBuilder.Entity<BankAccount>()
                        .HasRequired(o => o.Owner)
                        .WithMany(p => p.BankAccounts).Map(f => f.MapKey("TaxpayerId")).WillCascadeOnDelete();

            modelBuilder.Entity<Team>().Map(m => m.ToTable("aspnet_Teams"));
            modelBuilder.Entity<User>().Map(m => m.ToTable("aspnet_Membership"));
            
            modelBuilder.Entity<User>()
            .HasMany(t=>t.Teams).WithMany(u=>u.Users).Map(f =>
                {
                    f.ToTable("aspnet_Team2Users");
                    f.MapRightKey("TeamId");
                    f.MapLeftKey("PortalUserId");
                });
*/


        }
        }
    

    
}
